// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import "./assets/css/common.css";
import axios from "axios";
import vuex from "vuex";
import store from "./vuex/store.js";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI);
Vue.use(vuex);
Vue.use(store);
Vue.config.productionTip = false;
Vue.prototype.$axios = axios; //将axios挂载在Vue实例原型上

if (process.env.NODE_ENV == 'development') {
  //axios.defaults.baseURL = "http://ng.ngrok.caijinsuo.top:801/";
  // axios.defaults.baseURL = "http://192.168.1.12:8089"; //全
  axios.defaults.baseURL = "http://192.168.2.26:8089";
}
if (process.env.NODE_ENV == 'production') {
  axios.defaults.baseURL = "http://47.105.94.246:8089"; //线上
}


axios.interceptors.request.use(

  function (config) {
    // 在发送请求之前做些什么
    if (JSON.parse(localStorage.getItem("token"))) {
      let control = JSON.parse(localStorage.getItem("token"));
      if (control) {
        config.headers.token = control
      }
      return config;
    } else {
      return config;
    }

  }

);

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    if (response.data.code == 401) {
      localStorage.clear();
      router.push({ // 登录成功  路由跳转
        path: "/"
      });
      return false
    }
    return response
  },
  function (error) {
    //对响应错误做点什么
    return Promise.reject(error);
  }
);


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: {
    App
  },
  template: '<App/>'
})
